﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminCategorys : System.Web.UI.Page
    {
        private Int32 categoryID = 0;

        private Int32 pageIndex = 1;
        private Int32 pageSize = 20;
        private Int32 totalRecord;

        private DataTable dtCategorys = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                categoryID = Int32.Parse(Request.QueryString["CategoryID"].ToString());
            }
            catch (Exception ex)
            {
                categoryID = 0;
            }

            try
            {
                pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
            }
            catch (Exception ex)
            {
                pageIndex = 1;
            }

            if (!Page.IsPostBack)
            {
                dtCategorys = CategoryProvider.GetCategorysForBackEnd().Tables[0];

                CreateTreeViewCategory();

                btnNotAll.Visible = false;
                btnAll.Visible = true;
                
                FillDataIntoDataList(categoryID);

            }
        }

        /// <summary>
        /// Author : toanvt
        /// CreateDate : 31/01/2012
        /// Description : Phương thức tạo TreeView
        /// </summary>
        private void CreateTreeViewCategory()
        {
            TreeNode level0 = new TreeNode("All categorys", "0");
            CreateTreeViewCategorys(level0, 0);
            tvCategorys.Nodes.Add(level0);
        }

        /// <summary>
        /// Author : toanvt
        /// CreateDate : 31/01/2012
        /// Description : Phương thức đệ quy tạo TreeView
        /// </summary>
        private void CreateTreeViewCategorys(TreeNode treeNode, Int32 _categoryID)
        {
            DataRow[] dataRowsSubCateogrys = dtCategorys.Select("ParentID = " + _categoryID.ToString());

            Int32 amountOfSubCategory = dataRowsSubCateogrys.Count();

            //Điểm dừng đệ quy
            if (amountOfSubCategory == 0)
                return;

            TreeNode tn;

            for (Int32 i = 0; i < amountOfSubCategory; ++i)
            {
                Int32 subCategoryID = Int32.Parse(dataRowsSubCateogrys[i]["CategoryID"].ToString());
                tn = new TreeNode(dataRowsSubCateogrys[i]["CategoryName"].ToString(), subCategoryID.ToString());
                tn.CollapseAll();
                CreateTreeViewCategorys(tn, subCategoryID);
                treeNode.ChildNodes.Add(tn);

                if (subCategoryID == categoryID)
                {
                    treeNode.Expand();
                    tn.Select();
                }

                tn = null;
            }
        }

       /// <summary>
       /// Author : ChienNV
       /// CreatedDate : 31/01/2012
       /// Description : 
       /// </summary>
       /// <param name="CategoryID"></param>
       /// <param name="CategoryName"></param>
        public void FillDataIntoDataList(int CategoryID)
        {
            pageSize = this.PaggingControl1.PageSize;
            
            if(CategoryID!=0)
            {
                dtlCategoryParent.DataSource = CategoryProvider.GetCategorysByParentID(categoryID, pageIndex, pageSize, out totalRecord);
                dtlCategoryParent.DataBind();
            }
            else 
            {
                dtlCategoryParent.DataSource = CategoryProvider.GetAllCategorys(pageIndex, pageSize, out totalRecord);
                dtlCategoryParent.DataBind();
            }

            foreach (DataListItem dtl in dtlCategoryParent.Items)
            {
                TextBox txtstatus = (TextBox)dtl.FindControl("txtstatus");
                if (Boolean.Parse(txtstatus.Text) == true)
                {
                    ((ImageButton)dtl.FindControl("ImagCheck")).Visible = true;
                    ((ImageButton)dtl.FindControl("ImagUncheck")).Visible = false;
                }
                else if (Boolean.Parse(txtstatus.Text) == false)
                {
                    ((ImageButton)dtl.FindControl("ImagCheck")).Visible = false;
                    ((ImageButton)dtl.FindControl("ImagUncheck")).Visible = true;
                }

            }

            this.PaggingControl1.PageURL = "AdminCategorys.aspx?CategoryID=" + categoryID.ToString();
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PagingSize = pageSize;
        }
       
        // hàm xóa category theo checkbock
        protected void btnDel_Click(object sender, EventArgs e)
        {
            foreach (DataListItem dtl in dtlCategoryParent.Items)
            {
                bool check = ((CheckBox)dtl.FindControl("ckCategory")).Checked;
                if (check == true)
                {
                    TextBox txtID = ((TextBox)dtl.FindControl("txtID"));
                    try { 
                        int CategoryID = Int32.Parse(txtID.Text.ToString());
                        CategoryProvider.DeleteCategory(CategoryID);                        
                    }
                    catch(Exception ex){
                    
                    }
                }
               
            }
            Response.Redirect("~/UIAdmin/AdminCategorys.aspx");
            
        }

        protected void bntadd_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("~/UIAdmin/AdminCategory.aspx?ParentID=" + this.tvCategorys.SelectedNode.Value);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/UIAdmin/AdminCategory.aspx");
            }
        }


        // hàm ẩn button Not All
        protected void btnAll_Click(object sender, EventArgs e)
        {
            foreach (DataListItem dtl in dtlCategoryParent.Items)
            {                              
                    ((CheckBox)dtl.FindControl("ckCategory")).Checked = true;               
            }
            btnAll.Visible = false;
            btnNotAll.Visible = true;
        }

        // hàm ẩn button All
        protected void btnNotAll_Click(object sender, EventArgs e)
        {
            foreach (DataListItem dtl in dtlCategoryParent.Items)
            {
                ((CheckBox)dtl.FindControl("ckCategory")).Checked = false;
            }
            btnAll.Visible = true;
            btnNotAll.Visible = false;
        }

        // ham check all
        protected void ImagCheck_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            string CategoryID = btn.CommandArgument;

            Category theCate = CategoryProvider.GetCategoryByCategoryID(Int32.Parse(CategoryID.ToString()));
            Category theCategory = new Category();
            theCategory.CategoryID = theCate.CategoryID;
            theCategory.CategoryName = theCate.CategoryName;
            theCategory.DeletedFlag = theCate.DeletedFlag;
            theCategory.Description = theCate.Description;
            theCategory.Ordering = theCate.Ordering;
            theCategory.ParentID = theCate.ParentID;
            theCategory.PictureUrl = theCate.PictureUrl;
            theCategory.Status = false;
            CategoryProvider.IUCategory(theCategory, false);

            Response.Redirect("~/UIAdmin/AdminCategorys.aspx");

        }

        // hàm bỏ check all
        protected void ImagUncheck_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            string CategoryID = btn.CommandArgument;
            
            Category theCate = CategoryProvider.GetCategoryByCategoryID(Int32.Parse(CategoryID.ToString()));
            Category theCategory = new Category();
            theCategory.CategoryID = theCate.CategoryID;
            theCategory.CategoryName = theCate.CategoryName;
            theCategory.DeletedFlag = theCate.DeletedFlag;
            theCategory.Description = theCate.Description;
            theCategory.ParentID = theCate.ParentID;
            theCategory.Ordering = theCate.Ordering;
            theCategory.PictureUrl = theCate.PictureUrl;
            theCategory.Status = true;
            CategoryProvider.IUCategory(theCategory, false);

            Response.Redirect("~/UIAdmin/AdminCategorys.aspx");
        }

        protected void tvCategorys_SelectedNodeChanged(object sender, EventArgs e)
        {
            Response.Redirect("AdminCategorys.aspx?CategoryID=" + this.tvCategorys.SelectedNode.Value);
        }
    }
}